package com.industrial.governance.controller;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.governance.domain.QueryParams;
import com.industrial.governance.service.IDataAcquisitionService;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

/**
 * 数据采集统计
 */
@RestController
@RequestMapping("/data/acquisition")
public class DataAcquisitionController extends BaseController {

    @Autowired
    private IDataAcquisitionService dataAcquisitionService;

    /**
     * 查询数量
     * @return
     */
    @GetMapping("/queryCount")
    public AjaxResult queryCount(QueryParams params){

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else {
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(sysUser.getUserName().contains("admin")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince("370000");
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }

        Map<String, Object> resultMap = dataAcquisitionService.queryCount(params);

        return AjaxResult.success(resultMap);
    }


    /**
     * 查询年份的数据数量
     * @return
     */
    @GetMapping("/queryYearDataCj")
    public AjaxResult queryYearDataCj(QueryParams params){
        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            sysRoles.add("p_01_011");
        }

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCounty(sysUser.getDistrict());
            }else{
                params.setCounty(params.getCity());
                params.setCity(null);
            }
            params.setCity(null);
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else if(sysUser.getUserName().contains("admin")){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }

        Map<String, Object> resultMap = dataAcquisitionService.queryYearDataCj(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询各地市的数据数量
     * @return
     */
    @GetMapping("/queryCityDataCj")
    public AjaxResult queryCityDataCj(QueryParams params){
        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else{
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(sysUser.getUserName().contains("admin")){

                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince("370000");
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }

        Map<String, Object> resultMap = dataAcquisitionService.queryCityDataCj(params);
        return AjaxResult.success(resultMap);
    }
}
